import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';

import '../../resources/kq_theme_colors.dart';
import '../checkbox/kq_checkbox.dart';

/// 可勾选的按钮
class KqCheckedButton extends StatefulWidget {
  /// 是否勾选
  final bool isChecked;

  /// 是否部分选中，默认false
  final bool isHalfChecked;

  /// 是否build时自动设置_checked=isChecked
  final bool updateCheckedWhenBuild;

  /// 选中时的文字
  final String checkedString;

  /// 不选中是的文字
  final String unCheckedString;

  /// 文字大小
  final double? fontSize;

  /// 文字颜色
  final Color? textColor;

  /// CheckBox和文字之间的距离，默认8
  final double? spacing;

  /// 整体的水波纹圆角大小，默认2
  final double? buttonRadius;

  /// 整体背景颜色，默认透明
  final Color? backgroundColor;

  /// CheckBox的宽高，默认16
  final double? boxSize;

  /// 水平左右padding，默认2
  final double? horizontalPadding;

  /// 垂直左右padding，默认2
  final double? verticalPadding;

  /// checkbox 的样式，默认圆形
  final CheckBoxStyle checkBoxStyle;

  /// 选中改变监听
  final Function(bool checked)? onCheckedChanged;

  const KqCheckedButton(
      {Key? key,
      this.isChecked = false,
      this.isHalfChecked = false,
      required this.checkedString,
      required this.unCheckedString,
      this.onCheckedChanged,
      this.updateCheckedWhenBuild = false,
      this.backgroundColor,
      this.spacing,
      this.boxSize,
      this.buttonRadius,
      this.horizontalPadding,
      this.verticalPadding,
      this.checkBoxStyle = CheckBoxStyle.round,
      this.fontSize,
      this.textColor})
      : super(key: key);

  @override
  KqCheckedButtonState createState() => KqCheckedButtonState();
}

class KqCheckedButtonState extends State<KqCheckedButton> {
  late bool _checked;

  @override
  void initState() {
    _checked = widget.isChecked;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (widget.updateCheckedWhenBuild) {
      _checked = widget.isChecked;
    }
    return KqInkWell(
      radius: widget.buttonRadius ?? 2.r,
      verticalPadding: widget.verticalPadding ?? 2.r,
      horizontalPadding: widget.horizontalPadding ?? 2.r,
      backgroundColor: widget.backgroundColor ?? KqThemeColors.bgTransparent,
      onTap: widget.onCheckedChanged == null
          ? null
          : () {
              setState(() {
                _checked = !_checked;
                widget.onCheckedChanged?.call(_checked);
              });
            },
      child: Row(
        children: [
          KqCheckBox(
            value: _checked,
            width: widget.boxSize,
            height: widget.boxSize,
            style: widget.checkBoxStyle,
            isHalfChecked: widget.isHalfChecked
          ),
          SizedBox(
            width: widget.spacing ?? 8.r,
          ),
          Text(
            _checked ? widget.checkedString : widget.unCheckedString,
            style: TextStyle(
                fontSize: widget.fontSize ?? 14.sp,
                color: widget.textColor ?? KqThemeColors.text26),
          ),
        ],
      ),
    );
  }
}
